package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.ReportMapper;
import com.sky.service.ReportService;
import com.sky.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportMapper reportMapper;

    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {

        ArrayList<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        ArrayList<Double> turnoverList = new ArrayList<>();

        for (LocalDate localDate : dateList) {

            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);
            Double data = reportMapper.turnoverStatistics(beginTime, endTime, Orders.COMPLETED);
            data = data == null ? 0.0 : data;
            turnoverList.add(data);
        }
        TurnoverReportVO turnoverReportVO = new TurnoverReportVO().builder()
                .dateList(StringUtils.join(dateList, ","))
                .turnoverList(StringUtils.join(turnoverList, ","))
                .build();
        return turnoverReportVO;
    }

    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        ArrayList<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        ArrayList<Integer> totalUserList = new ArrayList<>();
        for (LocalDate localDate : dateList) {

            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);

            Integer totalUser = reportMapper.totalUserList(beginTime, endTime);
            totalUserList.add(totalUser);
        }

        ArrayList<Integer> newUserList = new ArrayList<>();
        for (LocalDate localDate : dateList) {

            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);

            Integer newUser = reportMapper.newUserList(beginTime, endTime);
            newUserList.add(newUser);
        }

        UserReportVO userReportVO = UserReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .totalUserList(StringUtils.join(totalUserList, ","))
                .newUserList(StringUtils.join(newUserList, ","))
                .build();
        return userReportVO;
    }

    @Override
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
        ArrayList<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        ArrayList<Integer> orderCountList = new ArrayList<>();
        for (LocalDate localDate : dateList) {

            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);

            Integer orderCount = reportMapper.orderCountList(beginTime, endTime);
            orderCountList.add(orderCount);
        }

        ArrayList<Integer> validOrderCountList = new ArrayList<>();
        for (LocalDate localDate : dateList) {

            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);

            Integer validOrderCount = reportMapper.validOrderCountList(beginTime, endTime);
            validOrderCountList.add(validOrderCount);
        }

        Integer total = orderCountList.stream().reduce(Integer::sum).get();

        Integer validTotal = validOrderCountList.stream().reduce(Integer::sum).get();

        Double orderCompletionRate = validTotal.doubleValue() / total;

        OrderReportVO orderReportVO = OrderReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .totalOrderCount(total)
                .orderCountList(StringUtils.join(orderCountList, ","))
                .validOrderCountList(StringUtils.join(validOrderCountList, ","))
                .validOrderCount(validTotal)
                .orderCompletionRate(orderCompletionRate)
                .build();
        return orderReportVO;
    }

    @Override
    public SalesTop10ReportVO top10(LocalDate begin, LocalDate end) {
//        ArrayList<OrderDetail> list2 = new ArrayList<>();
//
//        ArrayList<Orders> list = reportMapper.OrderList(LocalDateTime.of(begin, LocalTime.MIN), LocalDateTime.of(end, LocalTime.MAX));
//        for (Orders orders : list) {
//            ArrayList<OrderDetail> list1 = reportMapper.OrderDetailList(orders.getId());
//            list2.addAll(list1);
//        }
//        List<String> nameList = list2.stream().map(s -> s.getName()).distinct().collect(Collectors.toList());
//
//        HashMap<String, Integer> map = new HashMap<>();
//        for (OrderDetail orderDetail : list2) {
//            map.put(orderDetail.getName(), map.getOrDefault(orderDetail.getName(), 0) + orderDetail.getNumber());
//        }
//
//        ArrayList<Integer> numberList = new ArrayList<>(map.values());
        ArrayList numberList = new ArrayList();

        ArrayList nameList=new ArrayList();

        List<GoodsSalesDTO> goodsSalesDTOList = reportMapper.top10(LocalDateTime.of(begin, LocalTime.MIN), LocalDateTime.of(end, LocalTime.MAX));

        for (GoodsSalesDTO goodsSalesDTO : goodsSalesDTOList) {
            nameList.add(goodsSalesDTO.getName());
            numberList.add(goodsSalesDTO.getNumber());
        }


        SalesTop10ReportVO salesTop10ReportVO = SalesTop10ReportVO.builder()
                .numberList(StringUtils.join(numberList, ","))
                .nameList(StringUtils.join(nameList, ","))
                .build();
        return salesTop10ReportVO;
    }
}
